package com.goods.business.exception;

import com.goods.common.error.BusinessException;
import com.goods.common.response.ResponseBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author Decheng Kong
 * @date 2021/11/8 17:31
 */
@Slf4j
@RestControllerAdvice(basePackages = "com.goods.business.controller")
public class GlobalExceptionControllerAdvice {
    @ExceptionHandler(BusinessException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ResponseBean handleValidException(BusinessException e) {
        log.error(e.getErrorMsg());
        return ResponseBean.error(e);
    }


    @ExceptionHandler(value = Throwable.class)
    public ResponseBean handleException(Throwable throwable) {

        log.error("错误异常 {}", throwable);

        return ResponseBean.error(throwable);
    }
}
